﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnimalCare.Model;

namespace AnimalCare.Service
{
    class SupplierService
    {
        public static void Insert(string supplierName, string supplierPhone, string supplierInfo)
        {
            Supplier cf = new Supplier();
            cf.supplier_name = supplierName;
            cf.supplier_phone = supplierPhone;
            cf.supplier_info = supplierInfo;
            using (var ctx = new AnimalCareDBEntities())
            {
                ctx.Suppliers.AddObject(cf);
                ctx.SaveChanges();
            }
        }

        public static void Update(Supplier cf)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                var supplier = (from s in ctx.Suppliers
                                where s.supplier_id == cf.supplier_id
                                select s).FirstOrDefault();
                supplier.supplier_name = cf.supplier_name;
                supplier.supplier_phone = cf.supplier_phone;
                supplier.supplier_info = cf.supplier_info;
                int num = ctx.SaveChanges();
            }
        }

        public static void Delete(int id)
        {
            try
            {
                using (var ctx = new AnimalCareDBEntities())
                {
                    var cf = (from s in ctx.Suppliers
                              where s.supplier_id == id
                              select s).FirstOrDefault();
                    ctx.Suppliers.DeleteObject(cf);
                    int num = ctx.SaveChanges();
                }
            }
            catch
            {
            }
        }
    }
}
